package game.utils;

import game.frame.MainFrame;
import game.roles.bullets.*;
import game.roles.gamer.Gamer;
import game.roles.gamer.Helicopter;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * @author liuyulai
 * Created with IntelliJ IDEA.
 * Date: 2021/5/12
 * Time: 20:26
 * Description: No Description
 */
public class PressListenerTwo extends PressBase implements KeyListener {
//    public static boolean isUp;
//    public static boolean isDown;
//    public static boolean isLeft;
//    public static boolean isRight;
//
//    public static boolean isUp2;
//    public static boolean isDown2;
//    public static boolean isLeft2;
//    public static boolean isRight2;

    private int count;

    PressListenerOne p1 = new PressListenerOne();

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        playerOneMove(e);
        playerTwoMove(e);
    }


    @Override
    public void keyReleased(KeyEvent e) {
        playerOneAtt(e);
        playerTwoAtt(e);
    }

    /**
     * 修改将为后的玩家1按下移动
     *
     * @param e 键盘参数
     */
    private void playerOneMove(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            isUp = true;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            isDown = true;
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            isLeft = true;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            isRight = true;
        }
    }

    /**
     * 修改将为后的玩家二按下移动
     *
     * @param e 键盘参数
     */
    private void playerTwoMove(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            isUp2 = true;
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            isDown2 = true;
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            isLeft2 = true;
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            isRight2 = true;
        }
    }

    /**
     * 修改键位后的玩家二停止移动以及攻击
     *
     * @param e 键盘参数
     */
    private void playerTwoAtt(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            isUp2 = false;
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            isDown2 = false;
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            isLeft2 = false;
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            isRight2 = false;
        } else if (e.getKeyCode() == KeyEvent.VK_H) {
            releaseBoom();
        } else if (e.getKeyCode() == KeyEvent.VK_G) {
            bullet2Fire();
        }
    }

    /**
     * 修改键位后的玩家一停止移动以及攻击
     * 调用了PressListener1的部分攻击逻辑
     *
     * @param e 键盘参数
     */
    private void playerOneAtt(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            isUp = false;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            isDown = false;
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            isLeft = false;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            isRight = false;
        } else if (e.getKeyChar() == ',') {
            p1.releaseTanks();
        } else if (e.getKeyChar() == '/' && MainFrame.sp == 100) {
            p1.releaseSp();
        } else if (e.getKeyChar() == '.') {
            if (Gamer.iceBullet) {
                p1.bulletIce();
            } else {
                p1.bulletFire();
            }
        }
    }


    /**
     * 玩家二进行开火
     */
    private void bullet2Fire() {
        if (MainFrame.helicopter.getStatues() == Helicopter.GAMER2_FRONT) {
            //捡到特殊道具的情况下可以发射的弹幕
            if (Helicopter.allowFireByThree) {
                MainFrame.roleList.add(new NormalBullet(MainFrame.helicopter.x + 4, MainFrame.helicopter.y + 1, GamerBullet.RIGHT_UP));
                MainFrame.roleList.add(new NormalBullet(MainFrame.helicopter.x + 4, MainFrame.helicopter.y + 1, GamerBullet.RIGHT_DOWN));
                count++;
                //按下100次后回复普通发射的效果
                if (count >= 100) {
                    Helicopter.allowFireByThree = false;
                }
            }
            int x = MainFrame.helicopter.x;
            int y = MainFrame.helicopter.y;
            for (int i = 0; i < 3; i++) {
                MainFrame.roleList.add(new NormalBullet(x + 40, y + 100, GamerBullet.FRONT));
                x += 10;
                y += 10;
            }
        } else if (MainFrame.helicopter.getStatues() == Helicopter.GAMER2_BACK) {
            if (Helicopter.allowFireByThree) {
                MainFrame.roleList.add(new NormalBullet(MainFrame.helicopter.x - 4, MainFrame.helicopter.y + 1, GamerBullet.LEFT_UP));
                MainFrame.roleList.add(new NormalBullet(MainFrame.helicopter.x - 4, MainFrame.helicopter.y + 1, GamerBullet.LEFT_DOWN));
                count++;
                //按下15次后回复普通发射的效果
                if (count >= 100) {
                    Helicopter.allowFireByThree = false;
                }
            }
            int x = MainFrame.helicopter.x;
            int y = MainFrame.helicopter.y;
            for (int i = 0; i < 3; i++) {
                MainFrame.roleList.add(new NormalBullet(x - 20, y + 100, GamerBullet.BACK));
                x -= 10;
                y += 10;
            }
        }
    }

    /**
     * 玩家二进行开火
     */
    private void releaseBoom() {
        if (MainFrame.boomNum > 0) {
            MainFrame.helicopter.gamerSendBoom();
        }
    }
}
